package com.deliinsns.sys.listener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.deliinsns.app.basedata.bean.BaseData;
import com.deliinsns.sys.listener.SpringContext;
public class InitBasicDataFromDB {
	 ServletContext servletContext=SpringContext.servletContext;
	ApplicationContext context= SpringContext.getApplicationContext();
	HibernateTemplate hibernateTemplate=(HibernateTemplate) context.getBean("hibernateTemplate");
	
	public InitBasicDataFromDB(){
		  List<BaseData> jobclassifyList=  getBaseData("jobclassify");
		  Iterator<BaseData> it=  jobclassifyList.iterator();
		  BaseData bd= null;
		  Map<Integer,String> classifyMap= new HashMap<Integer,String>();
		  while(it.hasNext()){
			 bd=  it.next();
			 classifyMap.put(bd.getId() , bd.getValue());
		  }
		  List<BaseData> areaList=  getBaseData("area");
		   it=  areaList.iterator();
		  Map<Integer,String> areaMap= new HashMap<Integer,String>();
		  while(it.hasNext()){
			 bd=  it.next();
			 areaMap.put(bd.getId() , bd.getValue());
		  }
		  servletContext.setAttribute("jobclassifyMap", classifyMap);
		  servletContext.setAttribute("jobclassify", jobclassifyList);
		  servletContext.setAttribute("areaMap", areaMap);
		  servletContext.setAttribute("area", getBaseData("area"));
		  servletContext.setAttribute("school", getBaseData("school"));
		  servletContext.setAttribute("nation", getBaseData("nation"));
		  System.out.println("basedata has initialed!");
		  
	}
	private List<BaseData> getBaseData(String dataType){
		List<BaseData> baseDataList=hibernateTemplate.find("from BaseData b where b.dataType='"+dataType+"'");
		return baseDataList;
	}
	
}
